<?php

namespace App\Services\District;

use App\Models\DistrictModel;
use App\Services\AbstractBaseService;
use Illuminate\Support\Facades\Cache;


class DistrictService extends AbstractBaseService
{
    /**
     * 所有地址信息
     * @return array
     */
    public static function listAll()
    {
        return Cache::get('address-all-lists',function (){
            $select = ['id','adcode','name'];
            $list = DistrictModel::query()->where('level',1)->select($select)->orderBy('adcode')->get();
            foreach ($list as &$item){
                $list1 = DistrictModel::query()->where('pid',$item['id'])->select(['adcode','name'])->orderBy('adcode')->get();
                unset($item['id']);
                $item['adcode'] = $item['name'];
                $item['children'] = $list1;
            }
            return $list;
        });
    }
}
